package convert;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SpaceConverterGUI extends JFrame {
    private JTextArea inputArea;
    private JTextArea outputArea;
    private JButton convertButton;
    private JButton copyButton;

    public SpaceConverterGUI() {
        setTitle("空格转换工具");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 输入框
        inputArea = new JTextArea();
        inputArea.setLineWrap(true);
        inputArea.setBorder(BorderFactory.createTitledBorder("输入代码（支持多行）"));

        // 输出框
        outputArea = new JTextArea();
        outputArea.setLineWrap(true);
        outputArea.setEditable(false);
        outputArea.setBorder(BorderFactory.createTitledBorder("转换结果"));

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        convertButton = new JButton("转换");
        copyButton = new JButton("复制到剪贴板");
        buttonPanel.add(convertButton);
        buttonPanel.add(copyButton);

        // 布局
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                new JScrollPane(inputArea), new JScrollPane(outputArea));
        splitPane.setResizeWeight(0.5);
        add(splitPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        // 转换按钮事件
        convertButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String inputText = inputArea.getText();
                String processedText = processText(inputText);
                outputArea.setText(processedText);
            }
        });

        // 复制按钮事件
        copyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String outputText = outputArea.getText();
                if (!outputText.isEmpty()) {
                    StringSelection selection = new StringSelection(outputText);
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    clipboard.setContents(selection, null);
                    JOptionPane.showMessageDialog(null, "已复制到剪贴板！", "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
    }

    /**
     * 处理文本：
     * 1. 替换 Tab 为 2个全角空格
     * 2. 替换 2个半角空格为 1个全角空格
     */
    private String processText(String text) {
        // 先替换 Tab
        String tabReplaced = text.replace("\t", "　　");
        // 再替换连续2个半角空格
        return tabReplaced.replace("  ", "　");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SpaceConverterGUI().setVisible(true);
            }
        });
    }
}
